package com.portal.back.service.impl;

/**
 * @Title:后台菜单类service
 * @Copyright: Copyright (c) 0.1
 * @author:鲁根成
 * @version: 1.0
 * @time: 2010-3-25
 * 
 */
import java.util.ArrayList;
import java.util.List;

import com.portal.back.service.ISysMenuService;
import com.portal.back.web.DTO.TreeDto;
import com.portal.base.dao.BaseDao;
import com.portal.base.dao.IBaseDao;
import com.portal.domain.SysMenu;

public class SysMenuService implements ISysMenuService {
	private IBaseDao<SysMenu, Integer> dao;

	public SysMenuService() {
		dao = new BaseDao<SysMenu, Integer>(SysMenu.class);
	}

	public List<TreeDto> getByParentId(int parentId) {
		String sql = "select * from sys_menu where parentId=?";
		List<SysMenu> treeList = dao.findList(sql, new Object[] { parentId });
		List<TreeDto> dataList = new ArrayList<TreeDto>();
		if (null != treeList && treeList.size() > 0)
			for (int i = 0; i < treeList.size(); i++) {
				TreeDto td = new TreeDto();
				SysMenu sm = treeList.get(i);
				td.setHref(sm.getMenuUrl());
				td.setId(sm.getId());
				td.setQtip(sm.getMenuDescribe());
				td.setText(sm.getMenuName());
				List<TreeDto> smList = getByParentId(sm.getId().intValue());
				if (null != smList && smList.size() > 0)
					td.setChildren(smList);
				else
					td.setChildren(new ArrayList<TreeDto>());
				dataList.add(td);
			}
		return dataList;
	}

	public List<SysMenu> getAll() {
		String sql = "select * from sys_menu";
		return dao.findList(sql);
	}

	public boolean addMenu(String menuName, String menuUrl, String des,
			String parentid) {
		String sql = "insert into  sys_menu(menuName,menuUrl,menuDescribe,parentId) values(?,?,?,?) ";
		try {
			dao.insert(sql, new Object[] { menuName, menuUrl, des, parentid });
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}

	public boolean deleteMenuByid(String id) {
		String sql = "delete from sys_menu where  parentId=" + id;
		boolean isFlag = false;
		try {
			dao.delete(sql);
			isFlag = true;
		} catch (Exception e) {
			e.printStackTrace();
		}
		String sql2 = "delete from sys_menu where  id=" + id;
		boolean isflag = false;
		if (isFlag) {
			try {
				dao.delete(sql2);
				isflag = true;
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		if (isFlag && isflag)
			return true;
		return false;

	}

	public SysMenu getById(String id) {
		String sql = "select * from  sys_menu where id=?";
		List<SysMenu> list = dao.findList(sql, new Object[] { id });
		if (list != null && list.size() != 0)
			return list.get(0);
		return null;
	}

	public boolean editByid(String menuName, String menuUrl, String des,
			String id) {
		String sql = "update  sys_menu set menuName=?,menuUrl=?,menuDescribe=? where id=?";
		try {
			dao.update(sql, new Object[] { menuName, menuUrl, des, id });
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}
}
